<template>
	<view class="container">
		<!-- 用户名 -->
		<view v-if="flag==0">
			<view>用户名: </view>
			<input type="text" maxlength="7" v-model="userInfo.user.userName" />
		</view>
		<!-- 个性签名 -->
		<view v-if="flag==1">
			<view>个性签名: </view>
			<input type="text" maxlength="16" v-model="userInfo.user.gxSign" />
		</view>
		
		<view class="button" @click="handleSetUserInfo">确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flag: 0,//0是修改用户名，1是修改签名
				userInfo: { user: {}, params: {} },//当前用户信息
			}
		},
		methods: {
			//确认修改用户名
			async handleSetUserInfo () {
				if(this.flag == 0 ) {
					if(!this.userInfo.user.userName.trim().length) return uni.showToast({ title: '请输入名称~', icon: 'none' })
				}else {
					if(!this.userInfo.user.gxSign.trim().length) return uni.showToast({ title: '请输入个人签名~', icon: 'none' })
				}
				uni.setStorageSync('userInfo', this.userInfo)
				// this.userObj.adCode = uni.getStorageSync('region').slice(0, 6)
				let res = await this.$fetch(this.$api.upd_user, {userName: this.userInfo.user.userName, gxSign: this.userInfo.user.gxSign}, 'POST')
				if (res.data == 1 ){
					uni.showToast({ title: '修改成功~', icon: 'none' })
					setTimeout(() => { uni.navigateBack({ delta: 1 }) }, 500)
				} else{
					return uni.showToast({ title: res.msg?res.msg:'修改失败', icon: 'none' })
				}
				
			},
			
		},
		onLoad(options) {
			this.flag = options.flag
			console.log('flag', this.flag)
			this.userInfo = uni.getStorageSync('userInfo')
		},
	}
</script>

<style lang="less" scoped>
	.container {
		padding: 0 30rpx;
		height: 100vh;
		overflow: hidden;
		background-color: #FFFFFF;
		& > view {
			& > view:first-child {
				// color: red;
			}
			input {
				margin: 30rpx 0;
				padding-bottom: 10rpx;
				border-bottom: 1px solid rgba(211,211,211,0.6);
				background: transparent;
			}
			
		}
		& > .button {
				position: fixed;
				left: 30rpx;
				top: 500rpx;
				border-radius: 7px;
				display: flex;
				align-items: center;
				justify-content: center;
				width: 690rpx;
				height: 90rpx;
				background-color: #FCC928;
				&:active {
					opacity: 0.7;
				}
			}
	}

</style>
